<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Thư viện</title>
<style type="text/css">
#listHinh img {
	height: 50px;
	width: 50px;
	border: thin groove #003366;
	margin-top: 10px;
	margin-right: 5px;
	margin-bottom: 10px;
	margin-left: 5px;
}
#soFile {
	font-family: "Times New Roman", Times, serif;
	font-size: 14px;
	font-weight: bold;
	color: #003399;
}
.dmxtree {
	list-style-image: url(folderIcon.jpg);
	margin-left: 20px;
	font-family: "Times New Roman", Times, serif;
	font-size: 14px;
}
.dmxtree li {
	margin-top: 4px;
	margin-bottom: 4px;
}
#form1 {
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 2px;
	margin-left: 0px;
}
#form1 #url {
	font-family: "Times New Roman", Times, serif;
	font-size: 12px;
	font-style: italic;
	color: #660033;
	margin-right: 5px;
}
.dmxtree a {
	font-weight: bold;
	color: #003366;
	text-decoration: none;
	display: block;
	padding-left: 5px;
}
#form1 #btnUpload {
	height: 24px;
}
#form1 #file {
	height: 22px;
	background-color: #999999;
}
.dmxtree ul {
	list-style-image: url(folderIcon.jpg);
	position: relative;
	left: -20px;
}
body{
	font-family: Times New Roman, Times, serif;
	font-size: 14px;
	color: #006699;
	margin-top: 1px;
	margin-left: 1px;
	margin-right: 1px;
	margin-bottom: 1px;
}
.tenfile {
	font-family: "Times New Roman", Times, serif;
	font-size: 14px;
	color: #7F3F00;
	margin-right: 20px;
	margin-left: 20px;
	padding-top: 5px;
	padding-bottom: 5px;
	font-weight: bold;
	float: left;
}
</style>
</head>
<body >
<?php
	$what=in_array($_GET['what'],array('nhac','upload','quangcao','hinh'))? $_GET['what']:'upload'; 
  	if (isset($_POST['btnUpload'])){ 
  		  include "uploadFunction.php"; //what= upload / music / quangcao / hinh
 	}
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td bgcolor="#CC9900">Thư viện </td>
    <td align="right" bgcolor="#CC9900"><form id="form1" name="form1" enctype="multipart/form-data" method="post" action="">      
      <span id="url"></span>
      <input type="checkbox" name="phongto" id="phongto" />
      <label for="phongto">Phóng to</label>
      <input name="file" type="file" id="file" size="5" />
      <input type="submit" name="btnUpload" id="btnUpload" value="Upload" />
    </form></td>
  </tr>
  <tr>
    <td width="130" align="left" valign="top">
    <ul class="dmxtree" id="FolderView">
		<?php ListFolder("../".$what,$what); ?>
	</ul> </td>
    <td valign="top" bgcolor="#CCCC66">
    <div id="listHinh">
    <?	$soFile=0;
		$HienHinh=(in_array($what, array('hinh','quangcao','upload')))? 1:0;
    	if ((isset($_GET['subfolder']) && $_GET['subfolder']!="")) $soFile=ListFiles($_GET['subfolder'],$HienHinh);
	?>
    </div>     
    <div id=soFile>Có <?=$soFile;?> file</div>    </td>
  </tr>
</table>
<p>&nbsp;</p>
<style>
#hinhto {position: absolute; z-index: 2; height: 0px; width: 0px; left: 50px; top: 20px;}
.anhinh {display: none;	z-index: -1;}
</style>
<div id="hinhto"></div>
</body>
</html>
<?php
function ListFolder($path,$what)
{
    //using the opendir function
    $dir_handle = @opendir($path) or die("Không mở folder $path được");    
    $dirname = end(explode("/", $path));//Leave only the lastest folder name
    
    echo ("<li><a href=thuvien.php?what=$what&subfolder=$path>$dirname</a>\n");//display the target folder.
    echo "<ul>\n";
    while (false !== ($file = readdir($dir_handle)))  {
        if($file!="." && $file!="..") {
            if (is_dir($path."/".$file)) ListFolder($path."/".$file,$what);                    
        }
    } //while
    echo "</ul>\n";
    echo "</li>\n";    
    closedir($dir_handle); //closing the directory
}
function ListFiles($path,$HienHinh){
    $dir_handle = @opendir($path) or die("Không mở folder $path được");  
	$dem=0;
    while (false !== ($file = readdir($dir_handle))) {
        if($file!="." && $file!="..") {
            if (is_dir($path."/".$file)==false) {
				$fullPath=$path."/".$file;
				if ($HienHinh==1){
                echo "<img src=$fullPath onmouseover=\"document.getElementById('url').innerText='".$fullPath."'; ";
				echo "if (form1.phongto.checked) phongto(this);\" onDblClick='chonFile(\"$fullPath\")' >\n"; 
				}
				else {
					echo "<a class=tenfile href=# title=\"$fullPath\" onclick='chonFile(\"$fullPath\");return false;'>".substr($file,0,16)."...</a>";
				}
				$dem++;
				
			}
        }
    }       
    closedir($dir_handle);
	return $dem;
}

?>
<script>
	function chonFile(url){	
			window.opener.GanGiaTri(url);
			window.close();
	}
	function anPanel() {
		document.getElementById('hinhto').style.visibility="hidden";
	}	
	function phongto(obj) {
		document.getElementById('hinhto').style.width="400";
		document.getElementById('hinhto').style.height="300";
		document.getElementById('hinhto').style.visibility ="visible";	
		document.getElementById('hinhto').innerHTML="<img width=100% onclick='anPanel();' src="+ obj.src +">";

}
</script>